package cn.kduck.webapp.user.service.impl;

import cn.kduck.core.dao.query.QuerySupport;
import cn.kduck.core.service.DefaultService;
import cn.kduck.core.service.Page;
import cn.kduck.module.account.service.AccountCredentialProvider;
import cn.kduck.module.account.service.AccountUser;
import cn.kduck.module.user.exception.UserNotFoundException;
import cn.kduck.module.user.service.User;
import cn.kduck.module.user.service.UserService;
import cn.kduck.webapp.user.query.UserAccountQuery;
import cn.kduck.webapp.user.service.UserAccount;
import cn.kduck.webapp.user.service.AccountUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.Map;

@Service
public class AccountUserServiceImpl extends DefaultService implements AccountUserService, AccountCredentialProvider {

    @Autowired
    private UserService userService;

    @Override
    public List<UserAccount> listUserAccount(Map<String, Object> paramMap, Page page) {
        QuerySupport query = super.getQuery(UserAccountQuery.class, paramMap);
        return super.listForBean(query,page, UserAccount::new);
    }

    @Override
    public AccountUser getUserByPhone(String phone) {
        User user = userService.getUserByPhone(phone);
        if(ObjectUtils.isEmpty(user)){
            return null;
        }
        return new AccountUser(user.getUserId(),user.getUserName());
    }

    @Override
    public AccountUser getUserByEmail(String email) {
        User user = userService.getUserByEmail(email);
        if(ObjectUtils.isEmpty(user)){
            return null;
        }
        return new AccountUser(user.getUserId(),user.getUserName());
    }
}
